var TimecodeUtils = {
	edl: {
		replace: function(text, timecodes){
		    var i = 0;
		    return text.replace(/(\d{2}:){3}\d{2}/g, function(){
		        return timecodes[i++] || "unknown";
		    });
		},
		get: function(text){
		    return text.match(/(\d{2}:){3}\d{2}/g);
		},
		shift: function(timecodes, time, twoDigitMs) {
			if (!timecodes) {
				return timecodes;
			}
			if (typeof timecodes == 'string') {
				timecodes = [timecodes];
			}
			var result = [];
			for (var i=0;i<timecodes.length; i++) { 
				var t = TimeUtils.stringToTime(timecodes[i]);
				var r = TimeUtils.timeToString( t+time, twoDigitMs );
				//console.log(t+' -> '+r) 
				result.push( r )
			}
			return result;
		}
	}, 
	srt: {
		get: function(text) {
			return text.match(/(\d+):(\d+):(\d+),(\d+)/g);
		},
		 
		shift: function(time,amount) {
			 
		},
		
		/**
		 * Creates a SRT timecode from an EDL or generic timecode:
		 * 00:00:00:00 -> 00:00:00,000
		 */
		tcFrom: function(tc) {
			var p = tc.split(':');
			if (p.length == 4) {
				return p[0]+':'+p[1]+':'+p[2]+','+p[3]+'0'
			}
			return null;
		},
		
		tc2ms: function(tc) {
			var hh,mm,ss,ms; 
			var pq = tc.split(':');	
			hh = Number(pq[0]);
			mm = Number(pq[1]);
			ss = parseFloat(pq[2].replace(',','.'));
			//console.log('ss: '+Number(pq[2].replace(',','.')))
			//ss = Number(pq[2].split(',')[0]);
			//ms = Number(pq[2].split(',')[1]); 
			return hh*60*60*1000 + mm*60*1000 + ss*1000	;//+ ms;
		},
		ms2tc: function(ms) {
			var two = function(x) {return ((x>9)?"":"0")+x};
			var three = function(x) {return ((x>99)?"":"0")+((x>9)?"":"0")+x};
			
			var sec = Math.floor(ms/1000);
			ms = ms % 1000; 
			
			t = three(ms);
			var min = Math.floor(sec/60);
			sec = sec % 60;
			t = two(sec) + "," + t;

			var hr = Math.floor(min/60);
			min = min % 60;
			t = two(min) + ":" + t;

			//var day = Math.floor(hr/60)
			hr = hr % 60;
			t = two(hr) + ":" + t;
			//t = day + ":" + t

			return t;
		}
	}
}
var TCUtils = TimecodeUtils;